/*
 * S16-商品管理
 * @author fsl - gonglong
 */
$(function () {

	// 渲染页面-通用部分；
	commTemplUtil.initPage();
	// 渲染左侧菜单;
	commTemplUtil.initCommLeftMemu();

	var defaultPagingObj = {
		total : 0,
		pageSize : 10,
		page : 1
	};

	PAGE_DATA.pagingObj = $.extend(defaultPagingObj, PAGE_DATA.pagingObj);
	
	getGoodsData(1);
	//  新增商品 ,本店分类，状态;
	setPageDataOnce();
	// 全选/ 删除
	bindEventOnce();
	commTemplUtil.setLeftMenu(11);
});

function getGoodsData(page) {
	
	var onClickObj = {
		onClick : function () {
			var clickPage = $(this).attr("data-page");
			getGoodsData(clickPage);
		}
	};
	
	var goodListTempl = _.template($('#good-list-templ').html());
	
	// 获取 分类信息列表
	$.ajax({
		url : PAGE_CONFIG.requestUrl.goodsInfoList,
		type : "get",
		data : {
			page : parseInt(page || 0),
			pageSize : parseInt(PAGE_DATA.pagingObj.pageSize || 0),
			goodName : $("#good-name").val() || "",
			state : $("#goods-state").val() || "",
			classify : $("#goods-classify").val() || ""
		},
		success : function (result) {
			
			renderGoodsInfoList(result.data, goodListTempl);

			//  更新翻页控件
			var tmpConf = $.extend(result.pagingConfData, onClickObj);
			commTemplUtil.initPaging("#comm-paging", tmpConf);

		},
		error : function () {
			commTemplUtil.showErrorDialog({
				title : '注意！',
				message : '获取 分类信息 失败！',
				ok_txt : "确定"
			});
		}
	});
};


function renderGoodsInfoList(data, templ) {
	var listHtml = templ(data);
	$("#sel-all-chk").prop("checked", false);
	$("#commTabs_2_body").empty().html(listHtml);
	bindEvent();
	
};

/**
 * 全选/ 删除，只绑定一次事件;
 */
function bindEventOnce() {
	
	var goodListTempl = _.template($('#good-list-templ').html());
	var delConfTemplHtml = _.template($("#del-confirm").html());
	// 全选;
	var selAllChk = $("#sel-all-chk");
	var commTabBody = $("#commTabs_2_body");
	
	selAllChk.on("click", function () {
		var chkVal = $(this).prop("checked");
		commTabBody.find(":checkbox").prop("checked", chkVal);
	});



	// 删除多条数据,goodid, TODO,
	$("#del-all").on("click", function () {
		var goodsid = "";
		var goodIdAry = [];
		commTabBody.find(":checkbox:checked").each(function (index, ele) {
			goodIdAry.push($(this).attr("goodid"));
		});
		goodsid = goodIdAry.join(",");

		if (goodIdAry.length == 0) {
			// 未选择任何数据;
			commTemplUtil.showErrorDialog({
				title : "提示!",
				message : "请先选择数据！",
				ok_txt : "确定"
			});

			return false;
		}

		var deleDialog = dialog({
				id : "del-good-dialog",
				content : delConfTemplHtml,
				onshow : function() {
					var deleDiaBody = $("[id='content:del-good-dialog']");
					deleDiaBody.find("#ok-btn").on("click", function(){
						delMultDone(deleDialog, goodIdAry);
					});
					
					deleDiaBody.find("#cancel-btn").on("click", function(){
						deleDialog.remove();
					});
				}
			});

		deleDialog.showModal();

	});
	
	function delMultDone(diaObj, goodsId) {
		$.ajax({
			url : PAGE_CONFIG.requestUrl.delGood,
			type : "delete",
			data : {
				id : goodsId,
				_token : COMM_DATA.csrfToken
			},
			success : function (result) {
				diaObj.remove();
				renderGoodsInfoList(result.data, goodListTempl);
			},
			error : function () {

				diaObj.remove();
				commTemplUtil.showErrorDialog({
					title : "注意！",
					message : "删除数据 失败！",
					ok_txt : "确定"
				});
			}
		});
	};
		
	//  搜索  
	$("#search-btn").on("click", function(){
		getGoodsData(1);
	});
};

/**
 *  新增商品 ,本店分类, 状态;
 */
function setPageDataOnce(){
	
	$("#new-good").attr("href", PAGE_DATA.newGoodUrl || "");
	
	var optionModal = " <% _.each(obj, function(item){ %> <option value='<%=item.code%>'> <%=item.text%></option> <% }); %>";
	var optionTempl = _.template(optionModal);
	
	// 本店分类
	var classifyData = $.extend([], PAGE_DATA.classifyData);
	var classifyHtml = optionTempl(classifyData);
	$("#goods-classify").append(classifyHtml);
	
	// 状态
	var stateData = $.extend([], PAGE_DATA.stateData);
	var stateHtml = optionTempl(stateData);
	$("#goods-state").append(stateHtml);
	
};

/**
 * 绑定 删除 单条 数据;
 */
function bindEvent() {
	
	var goodListTempl = _.template($('#good-list-templ').html());
	var delConfTemplHtml = _.template($("#del-confirm").html());
	var commTabBody = $("#commTabs_2_body");
	var selAllChk = $("#sel-all-chk");
    
    // 取消全选
	commTabBody.find(":checkbox").on("change", function () {
		var unChk = commTabBody.find(":checkbox:not(:checked)").length;
		selAllChk.prop("checked", (unChk == 0));
	});
    
	// 删除 单条 数据,
	commTabBody.find(".delete-item").on("click", function () {

		var dataId = $(this).parent().attr("data-id");
		var goodId = [];
		goodId.push(dataId);

		var deleDialog = dialog({
				id : "del-single-dialog",
				content : delConfTemplHtml,
				onshow : function(){
					var deleDiaBody = $("[id='content:del-single-dialog']");
					deleDiaBody.find("#ok-btn").on("click", function(){
						doDele(deleDialog, goodId);
					});
					
					deleDiaBody.find("#cancel-btn").on("click", function(){
						deleDialog.remove();
					});
				}
			});

		deleDialog.showModal();


	});
	
	function doDele(diaObj, goodId) {

		$.ajax({
			url : PAGE_CONFIG.requestUrl.delGood,
			type : "delete",
			data : {
				id : goodId,
				_token : COMM_DATA.csrfToken
			},
			success : function (result) {

				diaObj.remove();
				renderGoodsInfoList(result.data, goodListTempl);
			},
			error : function () {
				diaObj.remove();
				commTemplUtil.showErrorDialog({
					title : "注意！",
					message : "删除失败！",
					ok_txt : "确定"
				});
			}
		});
	};
};


